package com.ecoolex.union.pay.retailer.admin.graphql;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Result;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.BeanUtil;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.annotation.AdminAuthoritativeRegister;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.union.pay.retailer.admin.core.AdminConstant;
import com.ecoolex.union.pay.retailer.entity.UnionPayPopularMerchant;
import com.ecoolex.union.pay.retailer.pojo.AddPopularMerchantDTO;
import com.ecoolex.union.pay.retailer.pojo.EditPopularMerchantDTO;
import com.ecoolex.union.pay.retailer.pojo.UnionPayPopularMerchantWithProductDTO;
import com.ecoolex.union.pay.retailer.ws.UnionPayPopularMerchantService;
import com.google.common.collect.Lists;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.lang.NonNull;

@GraphQL2Module(type = GraphQL2Module.Type.Admin, description = "爆款商户信息管理")
@AdminAuthoritativeRegister(parentKey = AdminConstant.AUTH_MODULE_KEY, key = UnionPayPopularMerchantGraphql.AUTH, name = "爆款商户信息管理")
public class UnionPayPopularMerchantGraphql {

    public static final String AUTH = AdminConstant.AUTH_MODULE_KEY + "-MERCHANT";

    public static final String AUTH_ADD = AUTH + "-ADD";

    public static final String AUTH_EDIT = AUTH + "-EDIT";

    public static final String AUTH_EDIT_PART = AUTH + "-EDIT-PART";

    public static final String AUTH_GET = AUTH + "-GET";

    public static final String AUTH_QUERY = AUTH + "-QUERY";

    @Reference
    UnionPayPopularMerchantService unionPayPopularMerchantService;

    @QueryField(field = "merchantList",description = "爆款商户列表")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_QUERY, name = "爆款商户列表")
    public Pagination<UnionPayPopularMerchantWithProductDTO> query(String keyword, @NonNull Page page, AdminMemberAuthority user) {
        user.assertAuth(AUTH_QUERY);
        return unionPayPopularMerchantService.queryMerchant(keyword,page);
    }


    @QueryField(field = "merchant",description = "爆款商户信息")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "爆款商户")
    public UnionPayPopularMerchant get(@NonNull Integer id, AdminMemberAuthority user) {
        user.assertAuth(AUTH_GET);
        return unionPayPopularMerchantService.get(id);
    }

    @MutationField(field = "addMerchant", description = "添加爆款商户")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD, name = "添加爆款商户")
    public UnionPayPopularMerchant add(@NonNull AddPopularMerchantDTO record, AdminMemberAuthority user) {
        user.assertAuth(AUTH_ADD);
        return unionPayPopularMerchantService.addMerchant(record, user.userName());
    }

    private static final String AUTH_DEL = AUTH + "-DEL";

    @MutationField(field = "delMerchant", description = "删除爆款商户")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_DEL, name = "删除爆款商户")
    public boolean del(@NonNull Integer id, AdminMemberAuthority user) {
        user.assertAuth(AUTH_DEL);
        UnionPayPopularMerchant merchant = unionPayPopularMerchantService.get(id);
        Assert.isTrue(Check.notNull(merchant), ResultCode.VERIFY.build(1), "爆款商户不存在");
        unionPayPopularMerchantService.delete(merchant, user.userName());
        return true;
    }

    @MutationField(field = "editMerchant", description = "编辑爆款商户")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT, name = "编辑爆款商户")
    public UnionPayPopularMerchant edit(@NonNull EditPopularMerchantDTO record, AdminMemberAuthority user) {
        user.assertAuth(AUTH_EDIT);
        UnionPayPopularMerchant old = unionPayPopularMerchantService.get(record.getId());
        Assert.isTrue(Check.notNull(old), ResultCode.VERIFY.build(1), "爆款商户不存在");
        String oldMerchantId = old.getMerchantId();
        BeanUtil.copy(record,
        		old,
                Lists.newArrayList(UnionPayPopularMerchant.Column.deleted, UnionPayPopularMerchant.Column.createUser, UnionPayPopularMerchant.Column.createTime).stream().map(
                        UnionPayPopularMerchant.Column::getJavaProperty).toArray(String[]::new));
        return unionPayPopularMerchantService.editMerchant(old,oldMerchantId, user.userName());
    }

    @QueryField(field="syncToCache", description="同步爆款数据到缓存")
    public Result syncToCache() {
        unionPayPopularMerchantService.syncToCache();
        return Result.createSuccessful();
    }
}
